package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.PowerBean;
import com.project.bean.RoleBean;
import com.project.bean.UserBean;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IRoleService;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleService implements IRoleService {
    @Autowired
    private IRoleMapper roleMapper;

    @Override
    public List<RoleBean> findRoleAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<RoleBean> cutByItem(Integer pageNO) {
        return roleMapper.selectPage(new Page<>(pageNO, 2), null);
    }

    @Override
    public void addRole(String roleName, List<Integer> powerIdList) {
        RoleBean roleBean = new RoleBean(roleName);
        roleMapper.insert(roleBean);
        roleMapper.addRole(roleBean.getId(), powerIdList);
    }

    @Override
    public void delRole(Integer roleId) {
        roleMapper.delRole(roleId);
        roleMapper.deleteById(roleId);

    }

    @Override
    public RoleBean findById(Integer roleId) {
        RoleBean roleBean = roleMapper.selectById(roleId);
        roleBean.setPowerList(roleMapper.findById(roleId));
        return roleBean;
    }

    @Override
    public void update(Integer id, List<Integer> powerId) {
       roleMapper.deletePower(id);
       roleMapper.addRole(id,powerId);
    }
}
